def gen01(len):
    print('wait to the first')
    for i in range(len):
        print(i)
        yield i * 10
        print('wait to generate the next')


def gen02(rows, columns):
    for j in range(1, rows):
        yield {'typed': 'index', 'value': j}
        for i in range(1, columns):
            yield {'value': i * 1000 + j}


def list02(rows, columns):
    for j in range(1, rows):
        print('\n', j, end=' - ')
        for i in range(1, columns):
            print(i * 1000 + j, end=' - ')


def gen03(rows, columns):
    for j in range(1, rows):
        s = yield j

        for i in range(1, columns):
            yield i * 1000 + j + s * 10


'''
key | int1| int2 | int3 | ...| list=[3,5,7] | dict={'k':int,'v':int} 

example:
index=1| 1001| 1002 |1003|...| list=[1,3,5] |dict={k:1,v:fib(1)}
'''


def gen04(rows, columns):
    s = fib(rows+3)
    for i in range(1, rows):
        # key
        yield 'index = ' + str(i)

        for j in range(1, columns):
            yield i * 1000 + j

        yield [2 * k + i for k in range(3)]

        yield {
            'k': i,
            'v': next(s)
        }
        # print(i)

def fib(max=100):
    a, b = 1, 1
    n = 1
    while (n <= max):
        yield a
        a, b = b, a + b
        n += 1
    return -1


def handle_item(k):
    if (isinstance(k, int)):
        print(k, end=' | ')
    elif (isinstance(k, list)):
        for i in k:
            print(i, end=' ~ ')
        print('', end=' | ')
    elif (isinstance(k, dict)):
        print(k, end=' | ')
    else:
        print(" error ")


if __name__ == '__main__':
    # r = gen01(4)
    # for i in range(5):
    #     print('yield ', next(r))
    # (7,3)
    print(3 in [2, 4, 5])
    print(({'dd': 3, 'ddd': 2}.get('ds')) == None)
    r = gen02(8, 4)
    # for i in range(len(r)):
    # 原则上不适合用for in 遍历迭代器，由于len（gen）是不存在的
    # for i in range(30):会造成抛出异常中断程序，如何不依赖28
    '''
    for i in range(28):
        k = next(r)
        # print(k)
        if ('typed' in k):
            print('\n', k['typed'], '=', k['value'], end=' : ')
        else:
            print(k['value'], end=' | ')
    '''

    while True:
        try:
            # print(next(r))
            k = next(r)

            if ('typed' in k):
                print('\n', k['typed'], '=', k['value'], end=' : ')
            else:
                print(k['value'], end=' | ')

        except StopIteration:
            break

    for k in gen02(7, 5):
        # print(k)
        if ('typed' in k):
            print('\n', k['typed'], '=', k['value'], end=' : ')
        else:
            print(k['value'], end=' | ')

    list02(8, 4)
    # ll = gen03(8, 4)
    # print(next(ll))
    # for i in range(28):
    #     print(ll.send(4))
    #
    for k in gen04(10, 4):
        if (isinstance(k, str)):
            print('\n', k.upper(), end=' : ')
        elif (isinstance(k, int)):
            print(k, end=' | ')
        elif (isinstance(k, list)):
            for i in k:
                print(i, end=' ~ ')
            print('', end=' | ')
        elif (isinstance(k, dict)):
            print(k, end=' | ')
        else:
            print(" error ")
    # 框架处理内容 需要使用者完成生成器 与 处理函数
    for k in gen04(10, 4):
        if (isinstance(k, str)):
            print('\n', k.upper(), end=' : ')
            print(k,end=' | ')
        else:
            handle_item(k)

    # 为什么这种只能取到index=8？
    # :todo